using System;
using System.Linq;
using Unity.Collections;
using Unity.Mathematics;
using UnityEngine;

public class VoxelDataForJob
{
    public static readonly bool[] isSolidArray = VoxelData.blockProperties.Select(s => s.isSolid).ToArray();

    public static readonly bool[] isHalfTransparentArray = VoxelData.blockProperties.Select(s => s.isHalfTransparent).ToArray();

    public static readonly float[] transparencyArray = VoxelData.blockProperties.Select(s =>  s.transparency).ToArray();

    public static readonly float2[] sideUvsArray = Array.ConvertAll(VoxelData.blockProperties.Select(s => s.sideUvs).ToArray().SelectMany(subArray => subArray).ToArray(), x => (float2)x);

    public static readonly float3[] voxelVerts = Array.ConvertAll(VoxelData.voxelVerts, x => (float3)x);

    public static readonly float3[] waterVoxelVerts = Array.ConvertAll(VoxelData.waterVoxelVerts, x => (float3)x);

    static int[] _voxelTris = new int[VoxelData.voxelTris.Length];
    static bool _isInitialized;
    public static int[] voxelTris
    {
        get{
            if (!_isInitialized)
            {
                _isInitialized = true;
                Buffer.BlockCopy(VoxelData.voxelTris, 0, _voxelTris, 0, _voxelTris.Length * sizeof(int));    
            }
            return _voxelTris;
        }
    }

    public static readonly float2[] voxelUvs = Array.ConvertAll(VoxelData.voxelUvs, x => (float2)x);

    public static readonly float3[] directions = Array.ConvertAll(VoxelData.directions, x => (float3)x);

    public static readonly NativeArray<int> voxelTrisN = new NativeArray<int>(voxelTris, Allocator.Persistent);
    public static readonly NativeArray<float3> voxelVertsN = new NativeArray<float3>(voxelVerts, Allocator.Persistent);
    public static readonly NativeArray<float3> waterVoxelVertsN = new NativeArray<float3>(waterVoxelVerts, Allocator.Persistent);
    public static readonly NativeArray<float2> voxelUvsN = new NativeArray<float2>(voxelUvs, Allocator.Persistent);
    public static readonly NativeArray<float3> directionsN = new NativeArray<float3>(directions, Allocator.Persistent);
    public static readonly NativeArray<float2> sideUvsN = new NativeArray<float2>(sideUvsArray, Allocator.Persistent);
    public static readonly NativeArray<float> transparencyN = new NativeArray<float>(transparencyArray, Allocator.Persistent);
    public static readonly NativeArray<bool> isSolidN = new NativeArray<bool>(isSolidArray, Allocator.Persistent);
    public static readonly NativeArray<bool> isHalfTransparentN = new NativeArray<bool>(isHalfTransparentArray, Allocator.Persistent);
    ~VoxelDataForJob()
    {
        voxelTrisN.Dispose();
        voxelVertsN.Dispose();
        waterVoxelVertsN.Dispose();
        voxelUvsN.Dispose();
        directionsN.Dispose();
        sideUvsN.Dispose();
        transparencyN.Dispose();
        isSolidN.Dispose();
        isHalfTransparentN.Dispose();
    }
}
